/*******************************************************************************
* CHIPSEA F61 IIC HAL
*******************************************************************************/

#include "hal_aw_86802_iic.h"

#ifdef AW_86802

#include <stdint.h>

#include "user_interface.h"

volatile uint8_t iicErrTime = 0;

/**********************************************************************
@brief : IIC 初始�?
@param : NONE
***********************************************************************/
void User_IIC_Slave_Init(void)
{
	I2C_HANDLE_TYPE_S iic_init_s;

	iic_init_s.i2c_irq_pri_e = AW_PRIORITY_0;
	iic_init_s.mode_e = I2C_MODE_SLAVE;
	iic_init_s.init_s.i2c_addr = IIC_ADDR;
	iic_init_s.init_s.i2c_speed_e = I2C_FAST_MODE;
	iic_init_s.init_s.fifo_rx_tl = I2C_FIFO_RX_TL_NUM;
	iic_init_s.init_s.fifo_tx_tl = I2C_FIFO_TX_TL_NUM;
	iic_init_s.init_s.sda_setup = I2C_SDA_SETUP;
	iic_init_s.init_s.spklen = FS_SPKLEN_NUM;
	iic_init_s.init_s.scl_hcnt = I2C_FS_SCL_HCNT;
	iic_init_s.init_s.scl_lcnt = I2C_FS_SCL_LCNT;
	iic_init_s.init_s.sda_hold = I2C_SDA_HOLD;

	i2c_init(&iic_init_s);
}

/***********************************************************************
@brief : 初始化IIC相关的状态，如地�?匹配，等待寄存器地址，一次�?�信完成
@param : NONE
************************************************************************/
/*
static void I2C_State_Init(void)
{
	IIC_State.Is_Wait_RegAddr = YES;
	IIC_State.I2C_Cmd_Done    = NO;
	IIC_State.I2C_SlaveAddr_Matched = NO;
}
*/

/***********************************************************************
@brief : I2C ISR
@param : NONE
************************************************************************/
volatile AW_U32 g_i2c_event = I2C_NOEVENT;

void IIC0_IRQnHandler(void)
{
	uint8_t ret  = 0x00;
	uint8_t data = 0x00;
	
	AW_U32 iic_falg = AW_NUM_NULL;
	AW_U32 iic_status = I2C0->INT_STATUS; /* Get interrupt status */

// GPIO_WriteBits(GPIO_PIN_10, 1);

	st_power.state = I2C_START;

	(void)iic_falg;
	/* Start signal interruption  */
	if ((iic_status & I2C_START_DET) == I2C_START_DET) 
	{
		iic_falg = I2C0->CLR_STAR_DET_INT;
		g_i2c_event = I2C_STAR;
	}

	/* Master write data interruption  */
	if ((iic_status & I2C_RX_FULL) == I2C_RX_FULL) 
	{
		if((I2C0->STATUS & I2C_STATUS_RFNE) == I2C_STATUS_RFNE) // FIFO has data
		{ 
			ret = (AW_U8)I2C0->DA_BUF_CMD;
		}

		if(st_iic.control_flag == false)                 //reg addr
		{
			st_iic.control_flag = true;

			register_select(ret);
			st_iic.w_pointer = 0;
		}
		else
		{
			if(st_iic.w_pointer < st_iic.w_count)       //write data to reg 
			{
				*(st_iic.data_ptr + st_iic.w_pointer) = ret;           
				st_iic.w_pointer++;
			}
		}
		g_i2c_event = I2C_RX_BUSY;
	}
	if ((iic_status & I2C_RX_OVER) == I2C_RX_OVER) {
		iic_falg = I2C0->CLR_RX_OV_INT;
	}

	/* Master read data interruption  */
	if((iic_status & I2C_RD_REQ) == I2C_RD_REQ) {

		iic_falg = I2C0->CLR_TX_ABRT_INT;

		if(st_iic.r_pointer < st_iic.r_count)
		{
			data = *(st_iic.data_ptr + st_iic.r_pointer);
			st_iic.r_pointer++;
		}

		I2C0->DA_BUF_CMD = data;

		I2C_SET_MASK(I2CIM_TX_EMPTY); // enable txEmpty interrupt
		iic_falg = I2C0->CLR_RD_REQ_INT; // clear CLR_RD_REQ flag
		g_i2c_event = I2C_TX_BUSY;
	}

	if (iic_status & I2CIM_TX_EMPTY) //tx fifo empty
	{ 
		iic_falg = I2C0->CLR_TX_ABRT_INT;

		if(st_iic.r_pointer < st_iic.r_count)
		{
			data = *(st_iic.data_ptr + st_iic.r_pointer);
			st_iic.r_pointer++;
		}

		I2C0->DA_BUF_CMD = data;
		g_i2c_event = I2C_TX_BUSY;
	}

	if (iic_status & I2CIM_TX_ABRT) 
	{
		iic_falg = I2C0->CLR_TX_ABRT_INT;
	}

	if (iic_status & I2CIM_RX_DONE) { // rd done
		// clean rx done interrupt
		iic_falg = I2C0->CLR_RX_DONE_INT;
		// send done, disable txEmpty
		I2C_CLEAN_MASK(I2CIM_TX_EMPTY); // disable txEmpty interrupt
		g_i2c_event = I2C_TX_BUSY;
	}

	/*check Stop event happen */
	if ((iic_status & I2C_STOP_DE) == I2C_STOP_DE) 
	{
		iic_falg = I2C0->CLR_STOP_DET_INT;//clear the STOP interrupt Flag
		iic_falg = I2C0->CLR_INT;

		st_iic.control_flag = false;
		st_iic.r_pointer = 0;
		st_iic.w_pointer = 0;

		switch (g_i2c_event)
		{
		case I2C_RX_BUSY:
			g_i2c_event = I2C_RX_END;
			break;

		case I2C_TX_BUSY:
			I2C0->INT_FLAG &= ~I2CIM_TX_EMPTY;
			g_i2c_event = I2C_TX_END;
			break;

		default:
			iic_falg= I2C0->CLR_STOP_DET_INT;//clear the STOP interrupt Flag
			iic_falg = I2C0->CLR_INT;
			g_i2c_event = I2C_NOEVENT;
			break;
		}
	}
	// GPIO_WriteBits(GPIO_PIN_10, 0);
}

/*******************************************************************************
@brief : I2C0_BusyError
@param : NONE
*******************************************************************************/
void hal_i2c_busy_error(void)
{

}

#endif /* End of CS_F61 */
